package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.Options;
import com.itheima.mapper.OptionsMapper;
import com.itheima.service.db.OptionsService;
import com.itheima.vo.OptionsVo;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class OptionsServiceImpl implements OptionsService {

    @Autowired
    private OptionsMapper optionsMapper;

    // 查询选项分数
    @Override
    public Integer findScore(Integer questionId, Integer optionId) {
        // 创建查询条件
        QueryWrapper<Options> qw = new QueryWrapper<>();
        qw.eq("quest_id", questionId).eq("options_Id", optionId);
        // 查询
        Options options = optionsMapper.selectOne(qw);
        Integer score = options.getScore();
        return score;
    }

    // 查选项集合
    @Override
    public List<OptionsVo> findByQuestionId(Integer soulQuestionId) {
        // 创建查询条件
        QueryWrapper<Options> qw = new QueryWrapper<>();
        qw.eq("quest_id", soulQuestionId);
        // 查询
        List<Options> optionsList = optionsMapper.selectList(qw);
        // 声明
        List<OptionsVo> voList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(optionsList)) {
            for (Options options : optionsList) {
                // 创建vo
                OptionsVo vo = new OptionsVo();
                vo.setId(options.getOptionsId().toString());
                vo.setOption(options.getName());
                // 添加到voList
                voList.add(vo);
            }
        }
        // 遍历
        return voList;
    }

    //查询答案
    @Override
    public PageBeanVo findSolution(Integer pageNum, Integer pageSize, Integer bankId) {
        //分页
        Page<Options> page = new Page<>(pageNum, pageSize);
        //条件
        QueryWrapper<Options> qw = new QueryWrapper<>();
        qw.eq("quest_id", bankId);
        page = optionsMapper.selectPage(page,qw );
        PageBeanVo pageBeanVo = new PageBeanVo(pageNum, pageSize, page.getTotal(), page.getRecords());
        return pageBeanVo;
    }

    //新增
    @Override
    public void saveSolution(Options options) {
        //条件
        QueryWrapper<Options> qw = new QueryWrapper<>();
        qw.eq("quest_id", options.getQuestId());
        //查询总数
        Integer count = optionsMapper.selectCount(qw);
        //维护选项id
        options.setOptionsId(count + 1);
        optionsMapper.insert(options);
    }

    //删除指定id
    @Override
    public void deleteSolutionById(Integer id) {
        optionsMapper.deleteById(id);
    }

    //删除多个选项
    @Override
    public void deleteMoreSolution(Integer[] ids) {
        if (ids != null){
            for (int i = 0; i < ids.length; i++) {
                optionsMapper.deleteById(ids[i]);
            }
        }
    }
}
